/*
 * This file is part of nettool4j.
 * 
 * rockframework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * rockframework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */
package net.woodstock.nettool4j.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.logging.Level;

import net.woodstock.nettool4j.AbstractRunnable;

public class OutputSocketHandler extends AbstractRunnable {

	private Socket	socket;

	public OutputSocketHandler(final Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			InputStream inputStream = this.socket.getInputStream();

			while (this.isRunEnabled()) {
				int i = -1;
				do {
					i = inputStream.read();
					if (i != -1) {
						if (Character.isLetterOrDigit(i)) {
							this.getLogger().log(Level.INFO, Integer.toString(i));
						}
						if (i == 255) {
							this.setRunEnabled(false);
							break;
						}
					}
				} while (i != -1);
			}

			this.socket.shutdownInput();
			this.socket.shutdownOutput();
			this.socket.close();
		} catch (IOException e) {
			this.getLogger().log(Level.WARNING, e.getMessage(), e);
		}
	}

}
